package bootstrap.liftweb

import net.liftweb._
import http.{LiftRules, Req, Html5Properties, NotFoundAsTemplate, ParsePath}
import sitemap.{SiteMap, Menu, Loc}
import util.{ NamedPF }
import net.liftweb._
import mapper.{Schemifier, DB, StandardDBVendor, DefaultConnectionIdentifier}
import util.{Props}
import common.{Full}
import http.{S}
import com.commutersconnect.model._
import com.commutersconnect.rest._
import com.commutersconnect.snippet._
import _root_.net.liftweb.sitemap.Loc._


//The majority of this file is auto generated from lifty.  The gyst is that
//Lift looks for a class 'bootstrap.liftweb.Boot' at startup and runs it.
//
//The tasks that are necessary to setup for this application are to define the
//main two pages in a sitemap, get the database started, and to finish any
//other setup that is typical for a Lift application.
class Boot {
  def boot {

    //This is the section that sets up the Mapper database items.  I do plan
    //on using the Mapper O/R so not ripping any of this out.  I did remove
    //the comment from lifty saying this is not required for lift. :)
    if (!DB.jndiJdbcConnAvailable_?) {
        val vendor =
            new StandardDBVendor(Props.get("db.driver") openOr "org.h2.Driver",
                    Props.get("db.url") openOr
                    "jdbc:h2:lift_proto.db;AUTO_SERVER=TRUE",
                    Props.get("db.user"), Props.get("db.password"))

            LiftRules.unloadHooks.append(vendor.closeAllConnections_! _)

            DB.defineConnectionManager(DefaultConnectionIdentifier, vendor)
    }
    Schemifier.schemify(true, Schemifier.infoF _, User, Report, Img, Message)

    // where to search snippet
    LiftRules.addToPackages("com.commutersconnect")

    // Our sitemap is relatively empty.  We are going for a site that has as
    // few page loads as possible.  Specifically, we have an index page that
    // shows a map of all reports, a report details page that will show a
    // report to you, and a page that lets you create a new report.
    val entries = List(
      Menu("Home") / "index",
      ViewReportSnippet.siteMenu,
      Menu("New Report") / "newReport"
      )
    LiftRules.setSiteMap(SiteMap(entries:_*))

    //We also need to initialize our rest classes.
    CCRestServices.init()
    NewFileUpload.init()

    //We are going to be using HTML5 templates, so we need to alert lift to
    //this fact.
    LiftRules.htmlProperties.default.set((r:Req) =>
        new Html5Properties(r.userAgent))


    //All of the following is provided by the lifty template with no
    //modifications.


    LiftRules.uriNotFound.prepend(NamedPF("404handler"){
      case (req,failure) => NotFoundAsTemplate(
        ParsePath(List("exceptions","404"),"html",false,false))
    })

    // set character encoding
    LiftRules.early.append(_.setCharacterEncoding("UTF-8"))

        // What is the function to test if a user is logged in?
        //TODO Determine how this is used...
    LiftRules.loggedInTest = Full(() => User.loggedIn_?)

    // Make a transaction span the whole HTTP request
    S.addAround(DB.buildLoanWrapper)
    //Show the spinny image when an Ajax call starts
    LiftRules.ajaxStart =
      Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd)

    // Make the spinny image go away when it ends
    LiftRules.ajaxEnd =
      Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd)

  }
}

